<template>
	<view class="content b-t">
		<view class="location-wrapper">
			<view class="tip">
				当前位置
			</view>
			<view class="location">
				<view class="left">{{currentAddress}}</view>
				<view class="right" @click="changeAddress"><i class="yticon icon-you"></i></view>
			</view>
		</view>
		<view class="amap">
			<map style="height:100%;width:100%" :show-location="showLocation" :scale="scale" :latitude="latitude" :longitude="longitude"
			 :markers="covers">
			</map>
		</view>

		<view class="m-list">
			<view class="list b-b" v-for="(item, index) in merchantList" :key="index"">
				<view class="wrapper" @click="findCover(item)">
					<view class="merchant-box">
						<text class="merchant">{{item.merchantName}}</text>
					</view>
					<view class="u-box">
						<text class="name">{{item.merchantAddress}}</text>
					</view>
					<view class="u-box">
						<text class="name">{{item.contactName}}</text>
					</view>
				</view>
				<view class="distance" @click="selectMerchant(item)">
					<view>{{item.distance}}公里</view>
					<view class="select-icon"><i class="yticon icon-dui"></i></view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniSwipeAction from '@/components/uni-swipe-action/uni-swipe-action.vue'
	import uniSwipeActionItem from '@/components/uni-swipe-action-item/uni-swipe-action-item.vue'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {
			uniSwipeAction,
			uniSwipeActionItem
		},
		data() {
			return {
				title: 'map',
				latitude: 30.6565202250,	//默认天府广场, 如果获取当前位置成功则替换为当前位置
				longitude: 104.0659332275,
				currentAddress:'',
				scale: 10,
				showLocation: true,
				covers: [],
				merchantList: []
			}
		},
		onLoad(option) {
			let that = this;
			console.log('QQ map key: '+that.$api.util.params.qqMapKey);
			uni.getLocation({
				type: 'gcj02',
				success: function(res) {
					console.log(JSON.stringify(res));
					that.latitude = res.latitude;
					that.longitude = res.longitude;
					uni.request({
						header:{
							"Content-Type": "application/text"
						},
						url:'https://apis.map.qq.com/ws/geocoder/v1/?location='+that.latitude+','+that.longitude+'&key='+that.$api.util.params.qqMapKey,
						success(geoRes) {
							console.log(JSON.stringify(geoRes));
							if(geoRes.statusCode === 200){
								that.currentAddress  = geoRes.data.result.address;
							}
						},
						fail(geoRes) {
							console.log(JSON.stringify(geoRes));
						}
					});
				},
				fail: function(res) {
					console.log(JSON.stringify(res));
				},
				complete: function(res) {
					that.inquiryNearbyMerchant(res.latitude, res.longitude);
				}
			});
		},
		computed: {
			...mapState(['hasLogin', 'hasMerchant', 'userInfo', 'merchantInfo'])
		},
		methods: {
			...mapMutations(['changeMerchant']),
			changeAddress(){
				let that = this;
				uni.chooseLocation({
					success: function (res) {
						if(res.name){
							that.currentAddress = res.address;
							that.latitude= res.latitude;
							that.longitude=res.longitude;
							that.inquiryNearbyMerchant(res.latitude, res.longitude);
						}
					}
				});
			},
			openLocation(item){
				uni.openLocation({
					latitude: item.latitude,
					longitude: item.longitude,
					success: function () {
						console.log('打开地图成功');
					}
				});
			},
			selectMerchant(item){
				this.changeMerchant(item);
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			findCover(val) {
				var covers = [];
				covers.push(this.getCover(val));
				this.covers = covers;
			},
			//根据一个门店产生标记点
			getCover(val) {
				return {
					latitude: val.latitude,
					longitude: val.longitude,
					width: 40,
					height: 40,
					iconPath: '../../static/image/address.png',
					callout: {
						content: val.merchantName,
						borderRadius: "10",
						bgColor: "#F44F31",
						color: '#FFFFFF',
						padding: 5,
						display: 'ALWAYS',
						textAlign: 'center'
					}
				}
			},
			//查询附近门店
			inquiryNearbyMerchant(lat, lng) {
				let that = this;
				this.$api.request.nearbyMerchant({
					userLatitude: lat||this.latitude,
					userLongitude: lng||this.longitude
				}, res => {
					if (res.body.status.statusCode === '0') {
						this.merchantList = res.body.data.merchants;
						var covers = [];
						this.merchantList.forEach(function(val, index) {
							covers.push(that.getCover(val));
						})
						this.covers = covers;
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				});
			}
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
	}

	.content {
		position: relative;
	}
	.location-wrapper{
		height: 120upx;
		padding: 20upx;
		.tip{
			font-size: $font-sm;
			color: $font-color-light;
		}
		.location {
			margin-top: 10upx;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			.left{
				font-size: $font-base;
				color: $font-color-dark;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
			.right{
				font-size: $font-base;
				color: $font-color-light;
				margin-left: 40upx;
			}
		}
	}
	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		;
		background: #fff;
		position: relative;

		.distance {
			color: $font-color-light;
			font-size: $font-base;
			text-align: right;
			.select-icon{
				margin-top: 30upx;
				color: $font-color-dark;
				i{
					font-size: $font-lg+8upx;
					color: $uni-color-primary;
				}
				
			}
			image {
				width: 60upx;
				height: 60upx;
			}
		}
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.merchant-box {
		display: flex;
		align-items: center;

		.tag {
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}

		.merchant {
			font-size: 30upx;
			color: $font-color-dark;
		}
	}

	.u-box {
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;

		.name {
			margin-right: 30upx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.amap {
		width: 100%; 
		height: 300px;
		left:0;
		z-index:999;
	}
	.m-list{
		width:100%;
		overflow-y: scroll;
	}
</style>
